package co.edu.udem.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import co.edu.udem.domain.Subject;
import co.edu.udem.domain.User;
import co.edu.udem.service.subject.SubjectPeriod;
import co.edu.udem.service.subject.SubjectService;
import co.edu.udem.service.user.UserServices;

/**
 * handles the login process.
 * 
 * @author sgranada
 * 
 */
@Controller
public class SubjectController extends BaseController {

	@Autowired
	private SubjectService subjectService;

	@Autowired
	private UserServices userService;

	private String currentUserCode;

	/**
	 * Handles and retrieves all persons and show it in a JSP page
	 * 
	 * @return the name of the JSP page
	 */
	@RequestMapping(value = "/subject/manage", method = RequestMethod.GET)
	public String getSubjectList(Model model) {

		logger.debug("Request recibido para mostrar todas las materias");
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		List<Subject> subjectList = subjectService
				.getSubjectListByTeacherCode(auth.getName().toUpperCase());
		currentUserCode = auth.getName().toUpperCase();
		model.addAttribute("subjects",
				subjectList == null ? new ArrayList<Subject>() : subjectList);
		model.addAttribute("currentUserCode", auth.getName());

		return "subjectpage";
	}

	/**
	 * Retrieves the add page
	 * 
	 * @return the name of the JSP page
	 */
	@RequestMapping(value = "/subject/add", method = RequestMethod.GET)
	public String getAdd(Model model) {
		logger.debug("Request recibido para mostrar la pagina de materias");
		model.addAttribute("subjectAttribute", new Subject());
		model.addAttribute("subjectPeriodList", SubjectPeriod.getCodesAsList());
		return "addsubjectpage";
	}

	/**
	 * Adds a new person by delegating the processing to PersonService. Displays
	 * a confirmation JSP page
	 * 
	 * @return the name of the JSP page
	 */
	@RequestMapping(value = "/subject/add", method = RequestMethod.POST)
	public String add(@ModelAttribute("subjectAttribute") Subject subject) {
		logger.debug("Request recibido para adicionar una materia");
		User user = userService.get(currentUserCode);
		subject.setTeacherCode(user.getUserCode());
		subject.setTeacherName(user.getName());
		subjectService.addSubject(subject);
		return "addedsubjectpage";
	}
	
	@RequestMapping(value = "/subject/edit", method = RequestMethod.GET)
	public String edit(@RequestParam("id") String subjectCode, Model model) {
		model.addAttribute("subjectAttribute", subjectService.getSubjectById(subjectCode));
		model.addAttribute("subjectPeriodList", SubjectPeriod.getCodesAsList());
		return "addsubjectpage";
	}
	
	
	@RequestMapping(value = "/subject/delete", method = RequestMethod.GET)
	public String delete(@RequestParam("id") String subjectCode) {
		subjectService.delete(subjectCode);
		return "deletedsubjectpage";
	}

}
